﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Windows.Input;
using System.Diagnostics;
using SPIWareMVVM.Views.Commands;
using System.Collections.ObjectModel;
using System.Threading;
using System.Security.Principal;

namespace SPIWareMVVM.ViewModels
{
    public class ApplicationViewModel : BaseViewModel
    {
        List<DateTime> _runDates { get; set; }
        public List<DateTime> RunDates
        {
            get
            {
                if (_runDates == null) _runDates = ApplicationData.ocProduction
                    .OrderByDescending(d => ((DateTime)d.EndTime).Date)
                    .Select(r => ((DateTime)r.EndTime).Date).Distinct().ToList();
                if (!_runDates.Contains(DateTime.Today)) _runDates.Insert(0, DateTime.Today);
                return _runDates;
            }
        }

        #region Fields (Private Properties)
        private string _applicationVersion { get; set; }
        Customer _selectedCustomer { get; set; }
        WorkOrder _selectedWorkOrder { get; set; }
        Part _selectedMaterial { get; set; }
        MachinePress _selectedPressMachine { get; set; }
        ApplicationUser _selectedUser { get; set; }
        #endregion

        public ObservableCollection<WorkOrder> WorkOrderList
        {
            get
            {
                if (SelectedCustomer != null)
                    return SelectedCustomer.WorkOrders.ToObservableCollection<WorkOrder>();
                else return ApplicationData.ocWorkOrders;
            }
        }

        public string ApplicationVersion
        {
            get
            {
                if (_applicationVersion == null)
                {
                    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
                    _applicationVersion = string.Format("v{0}", assembly.GetName().Version.ToString());
                }
                return _applicationVersion;
            }
        }
        public string ApplicationTitle { get { return string.Format("SPIWare {0}", this.ApplicationVersion); } }
        public string DatabaseConfigString
        {
            get
            {
                //"Data Source=148L\\SPIWareExpress;Initial Catalog=SPIWareIII;Integrated Security=True"
                string sStr = "Data Source=";
                string dStr = ";Initial Catalog=";
                string conn = ApplicationData.DataContext.Connection.ConnectionString;
                int sIndex = conn.IndexOf(sStr, 0);
                int dIndex = conn.IndexOf(dStr, sIndex);
                int iIndex = conn.IndexOf("\\", 0);

                string server = conn.Substring(sIndex + sStr.Length, iIndex - sIndex - sStr.Length);
                string instance = conn.Substring(iIndex + 1, dIndex - iIndex-1);
                string database = conn.Substring(dIndex + dStr.Length, conn.IndexOf(";Integrated Security") - dIndex - dStr.Length);
                return string.Format("{0} | {1} | {2}", server, instance, database);
            }
        }

        public Customer SelectedCustomer
        {
            get { return _selectedCustomer; }
            set
            {
                ApplicationData.SelectedCustomer = value;
                _selectedCustomer = value;
                OnPropertyChanged("SelectedCustomer");
            }
        }
        public WorkOrder SelectedWorkOrder { get { return _selectedWorkOrder; } set { ApplicationData.SelectedWorkOrder = value; _selectedWorkOrder = value; OnPropertyChanged("SelectedWorkOrder"); } }
        public Part SelectedMaterial { get { return _selectedMaterial; } set { ApplicationData.SelectedMaterial = value; _selectedMaterial = value; OnPropertyChanged("SelectedMaterial"); } }
        public MachinePress SelectedPressMachine { get { return _selectedPressMachine; } set { ApplicationData.SelectedMachinePress = value; _selectedPressMachine = value; OnPropertyChanged("SelectedPressMachine"); } }

        bool _userNeedsLoggedIn = true;
        public bool UserNeedsLoggedIn { get { return _userNeedsLoggedIn; } set { _userNeedsLoggedIn = value; OnPropertyChanged("UserNeedsLoggedIn"); } }
        public bool IsUserLoggedIn { get { return SelectedUser != null; } set { } }
        public ApplicationUser SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                ApplicationData.SelectedUser = value; _selectedUser = value;
                OnPropertyChanged("SelectedUser");
                OnPropertyChanged("UserNeedsLoggedIn");
                OnPropertyChanged("IsUserLoggedIn");
            }
        }
        public void AuthenticateUser(string userName, string passWord)
        {
            SelectedUser = ApplicationData.ApplicationUsers.Where(u => u.UserName == userName && u.Password == passWord).FirstOrDefault();
            if (SelectedUser != null)
            {
                UserNeedsLoggedIn = false;
                IsUserLoggedIn = true;
            }
            //testing rolebased command
            string[] roles = new string[] { "Admin", "Clerical" };
            Thread.CurrentPrincipal = new GenericPrincipal(WindowsIdentity.GetCurrent(), roles);
        }
        #region Commands
        //#region Database Commands
        //CreateDatabaseCommand _createDatabaseCommand { get; set; }
        //public CreateDatabaseCommand CreateDatabaseCommand
        //{
        //    get
        //    {
        //        if (_createDatabaseCommand == null) _createDatabaseCommand = new CreateDatabaseCommand(this);
        //        return _createDatabaseCommand;
        //    }
        //}
        //#endregion

        private ShowCustomerListCommand _showCustomerListCommand { get; set; }
        public ShowCustomerListCommand ShowCustomerListCommand
        {
            get
            {
                if (_showCustomerListCommand == null) _showCustomerListCommand = new ShowCustomerListCommand(this);
                return _showCustomerListCommand;
            }
        }

        public bool CustomerWOListAvailable { get { if (this.SelectedCustomer != null) return true; return false; } }
        ShowCustomerWOListCommand _showCustomerWOListCommand { get; set; }
        public ShowCustomerWOListCommand ShowCustomerWOListCommand
        {
            get
            {
                if (_showCustomerWOListCommand == null) _showCustomerWOListCommand = new ShowCustomerWOListCommand(this);
                return _showCustomerWOListCommand;
            }
        }

        ShowCustomerMaterialListCommand _showCustomerMaterialListCommand { get; set; }
        public ShowCustomerMaterialListCommand ShowCustomerMaterialListCommand
        {
            get
            {
                if (_showCustomerMaterialListCommand == null) _showCustomerMaterialListCommand = new ShowCustomerMaterialListCommand(this);
                return _showCustomerMaterialListCommand;
            }
        }

        private ShowWorkOrdersListCommand _showWorkOrderListCommand { get; set; }
        public ShowWorkOrdersListCommand ShowWorkOrderListCommand
        {
            get
            {
                if (_showWorkOrderListCommand == null) _showWorkOrderListCommand = new ShowWorkOrdersListCommand(this);
                return _showWorkOrderListCommand;
            }
        }

        ShowWODeliveriesListCommand _showWODeliveriesListCommand { get; set; }
        public ShowWODeliveriesListCommand ShowWODeliveriesListCommand
        {
            get
            {
                if (_showWODeliveriesListCommand == null) _showWODeliveriesListCommand = new ShowWODeliveriesListCommand(this);
                return _showWODeliveriesListCommand;
            }
        }

        private ShowMaterialsListCommand _showMaterialsListCommand { get; set; }
        public ShowMaterialsListCommand ShowMaterialsListCommand
        {
            get
            {
                if (_showMaterialsListCommand == null) _showMaterialsListCommand = new ShowMaterialsListCommand(this);
                return _showMaterialsListCommand;
            }
        }

        ShowCommissionListCommand _showCommissionsListCommand { get; set; }
        public ShowCommissionListCommand ShowCommissionsListCommand
        {
            get
            {
                if (_showCommissionsListCommand == null) _showCommissionsListCommand = new ShowCommissionListCommand(this);
                return _showCommissionsListCommand;
            }
        }

        ShowDeliveryListCommand _showDeliveryListCommand { get; set; }
        public ShowDeliveryListCommand ShowDeliveryListCommand
        {
            get
            {
                if (_showDeliveryListCommand == null) _showDeliveryListCommand = new ShowDeliveryListCommand(this);
                return _showDeliveryListCommand;
            }
        }

        ShowPressMachineListCommand _showPressMachineListCommand { get; set; }
        public ShowPressMachineListCommand ShowPressMachineListCommand
        {
            get
            {
                if (_showPressMachineListCommand == null) _showPressMachineListCommand = new ShowPressMachineListCommand(this);
                return _showPressMachineListCommand;
            }
        }

        ShowProductionListCommand _showProductionListCommand { get; set; }
        public ShowProductionListCommand ShowProductionListCommand
        {
            get
            {
                if (_showProductionListCommand == null) _showProductionListCommand = new ShowProductionListCommand(this);
                return _showProductionListCommand;
            }
        }

        private SaveCommand _saveCommand { get; set; }
        public SaveCommand SaveCommand
        {
            get
            {
                if (_saveCommand == null) _saveCommand = new SaveCommand(this);
                return _saveCommand;
            }
        }

        private AddNewCustomerCommand _addNewCustomerCommand { get; set; }
        public AddNewCustomerCommand AddNewCustomerCommand
        {
            get
            {
                if (_addNewCustomerCommand == null) _addNewCustomerCommand = new AddNewCustomerCommand(this);
                return _addNewCustomerCommand;
            }
        }

        private AddNewWorkOrderCommand _addNewWorkOrderCommand { get; set; }
        public AddNewWorkOrderCommand AddNewWorkOrderCommand
        {
            get
            {
                if (_addNewWorkOrderCommand == null) _addNewWorkOrderCommand = new AddNewWorkOrderCommand(this);
                return _addNewWorkOrderCommand;
            }
        }

        private AddNewPartCommand _addNewPartCommand { get; set; }
        public AddNewPartCommand AddNewPartCommand
        {
            get
            {
                if (_addNewPartCommand == null) _addNewPartCommand = new AddNewPartCommand(this);
                return _addNewPartCommand;
            }
        }

        AddNewPressMachineCommand _addNewPressMachineCommand { get; set; }
        public AddNewPressMachineCommand AddNewPressMachineCommand
        {
            get
            {
                if (_addNewPressMachineCommand == null) _addNewPressMachineCommand = new AddNewPressMachineCommand(this);
                return _addNewPressMachineCommand;
            }
        }

        AddNewProductionCommand _addNewProductionCommand { get; set; }
        public AddNewProductionCommand AddNewProductionCommand
        {
            get
            {
                if (_addNewProductionCommand == null) _addNewProductionCommand = new AddNewProductionCommand(this);
                return _addNewProductionCommand;
            }
        }

        AddNewContactCommand _addNewContactCommand { get; set; }
        public AddNewContactCommand AddNewContactCommand
        {
            get
            {
                if (_addNewContactCommand == null) _addNewContactCommand = new AddNewContactCommand(this);
                return _addNewContactCommand;
            }
        }

        CompleteWOCommand _completeWOCommand { get; set; }
        public CompleteWOCommand CompleteWOCommand
        {
            get
            {
                if (_completeWOCommand == null) _completeWOCommand = new CompleteWOCommand(this);
                return _completeWOCommand;
            }
        }
        #endregion

        PrintReportsCommand _printReportsCommand { get; set; }
        public PrintReportsCommand PrintReportsCommand
        {
            get
            {
                if (_printReportsCommand == null) _printReportsCommand = new PrintReportsCommand(this);
                return _printReportsCommand;
            }
        }
    }
}
